package com.aispeech.jarpc.op.vad;

import com.aispeech.asr.comm.enums.EAudioCodec;
import com.aispeech.jarpc.TextBin;
import com.aispeech.jarpc.op.AbstractRequest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class OfflineRequest extends AbstractRequest {
    private final static int BUFF_LEN = 3200;
    private final static String REQ_TMPL = "{\"op\":\"vad\",\"audio-decoder\":{\"dst_rate\":%d,\"audio_type\":%d,\"src_rate\":%d,\"channel\":%d,\"samplebytes\":%d},\"vad\":{\"cfg_path\":\"%s\",\"is_bin\":1,\"pause_time\":%d,\"max_speech_secs\":%d,\"need_pcm\":true,\"pcm_is_only_speech\":%b,\"pcm_path\":\"%s\"}}";

    private int destSampleRate;
    private EAudioCodec audioCodec;
    private int srcSampleRate;
    private int srcChannel;
    private int srcSampleBytes;
    private String vadBinPath;
    private int vadPauseTime;
    private int maxSpeechSecs;
    private String outputPcmPath;
    private boolean pcmIsOnlySpeech;
    private InputStream audioStream;

    public OfflineRequest(int destSampleRate, EAudioCodec audioCodec, int srcSampleRate, int srcChannel, int srcSampleBytes, String vadBinPath, int vadPauseTime, int maxSpeechSecs, String outputPcmPath, InputStream audioStream) {
        this.destSampleRate = destSampleRate;
        this.audioCodec = audioCodec;
        this.srcSampleRate = srcSampleRate;
        this.srcChannel = srcChannel;
        this.srcSampleBytes = srcSampleBytes;
        this.vadBinPath = vadBinPath;
        this.vadPauseTime = vadPauseTime;
        this.maxSpeechSecs = maxSpeechSecs;
        this.outputPcmPath = outputPcmPath;
        this.pcmIsOnlySpeech = false;

        this.audioStream = audioStream;
    }
    
    public OfflineRequest(int destSampleRate, EAudioCodec audioCodec, int srcSampleRate, int srcChannel, int srcSampleBytes, String vadBinPath, int vadPauseTime,boolean pcmIsOnlySpeech) {
        this.destSampleRate = destSampleRate;
        this.audioCodec = audioCodec;
        this.srcSampleRate = srcSampleRate;
        this.srcChannel = srcChannel;
        this.srcSampleBytes = srcSampleBytes;
        this.vadBinPath = vadBinPath;
        this.vadPauseTime = vadPauseTime;
        this.maxSpeechSecs = 30;
        this.pcmIsOnlySpeech = pcmIsOnlySpeech;
        this.outputPcmPath = "";
        this.audioStream = null;
    }

    public TextBin toJsonBin() throws IOException {
        String json = toJson();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[BUFF_LEN];

        int read;
        while ((read = audioStream.read(buff)) > 0) {
            byteArrayOutputStream.write(buff, 0, read);
        }

        if (audioStream.markSupported())
            audioStream.reset();

        return new TextBin(json, byteArrayOutputStream.toByteArray());
    }

    @Override
    public String toJson() {
        return String.format(REQ_TMPL, destSampleRate, audioCodec.value(), srcSampleRate, srcChannel, srcSampleBytes, vadBinPath, vadPauseTime, maxSpeechSecs, pcmIsOnlySpeech ,outputPcmPath);
    }

    @Override
    public String OpType() {
        return "vad";
    }
}
